<?php
  // no direct access
  defined( '_JEXEC' ) or die( 'Restricted access' );
  jimport('joomla.application.component.controller');
  
  class StandingsController extends JController
  {  
    function display(){
      
      $document =& JFactory::getDocument();
      $view = & $this->getView($this->_name, $document->getType());
      
      $season     = $this->getModel('season', 'seasonmodel');
      $division   = $this->getModel('division', 'divisionmodel');
      $conference = $this->getModel('conference', 'conferencemodel');
      
      $season_division     = $this->getModel('seasondivision', 'seasondivisionmodel');
      $division_conference = $this->getModel('divisionconference', 'divisionconferencemodel');
      $conference_team     = $this->getModel('conferenceteam', 'conferenceteammodel');
      $gs                  = $this->getModel('gamestat','gamestatmodel');

      $season_id = JRequest::getVar('id');
      $divisions        = array();
      
      if(!isset($season_id)){
        if($lseason = $season->get_latest())
          $season_id     = $lseason->id;
      }
        
      if($season_id){
        $season_standings = $gs->get_season_standings($season_id);
        $sds = $season_division->get_by_season_id($season_id);
          foreach($sds as $sd)
            array_push($divisions, $division->get($sd->division_id));
        
        for($i=0; $i<count($divisions); $i++){
        
          $d  = $divisions[$i];
          $dcs = $division_conference->get_by_division_id($d->id);
          
          if(!isset($divisions[$i]->conferences))
            $divisions[$i]->conferences = array();
            
          if($dcs){
          
            foreach($dcs as $dc){
            
              $c        = $conference->get($dc->conference_id);
              $c->teams = $conference_team->get_teams_by_conference_id($c->id);
              if(isset($season_standings)){
                foreach($c->teams as $team){
                  if(isset($season_standings[$team->id]))
                    $team->standing = $season_standings[$team->id];
                }
              }
              
              array_push($divisions[$i]->conferences, $c);
            }
            
          }
        }
      }
      
      $view->assign("divisions", $divisions);
      $view->display();
    }

    private function get_team_standings($conference_id, $season_id = 0){

      $team            = $this->getModel('team', 'teammodel');
      $season          = $this->getModel('season', 'seasonmodel');
      $conference_team = $this->getModel('conferenceteam', 'conferenceteammodel');
      $gs              = $this->getModel('gamestat','gamestatmodel');
      
      $teams = $conference_team->get_teams_by_conference_id($conference_id);
      
      foreach($teams as $t){
      
        if($season_id){
          $standing = $gs->get_season_standings($season_id);
          $t->standing = $standing;
        }
      }
      return $teams;
    }
  }
?>
